import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../helper/cacheHelper.dart';
import '../../pages/things/things_helper.dart';
import '../../widgets/dialogWidget.dart';

import '../../helper/routeHelper.dart';
import '../../helper/uidata.dart';
import '../../model/inventory.dart';

import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';

import '../../helper/Helper.dart';

class InventoryInfoPage extends StatefulWidget {
  InventoryInfoPage(this.cb1, this.added);
  final Inventory cb1;
  final VoidCallback added;

  @override
  InventoryInfoPageState createState() => new InventoryInfoPageState();
}

class InventoryInfoPageState extends State<InventoryInfoPage> {
  final String now = Helper.DateTimeNowString();

  bool ifLoad = false;
  late Inventory s1;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData() async {
    var xx = await Inventory.GetByID(widget.cb1.ID);

    setState(() {
      ifLoad = true;
      s1 = xx;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("物资单详情"),
      ),

      body: ifLoad
          ? CleanInfoBody(context)
          : Center(
              child: SpinKitCircle(
              color: Colors.blue,
            )),
    );
  }

  CleanInfoBody(BuildContext context) => SingleChildScrollView(
        child: CleanInfoFields(context),
      );

  String getSS() {
    String bz2 = "";
    if (s1.State == 0) bz2 = bz2 + "待审核";
    if (s1.State == 1) bz2 = bz2 + "已审核";
    if (s1.State == 2) bz2 = bz2 + "已归还";
    if (s1.State == 3) bz2 = bz2 + "已损毁";
    return bz2;
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    List<Widget> list6 = <Widget>[];
    list2.add(InputWidget.getInfo("单据序号", s1.ID.toString()));
    list2.add(InputWidget.getDivider());
    list2.add(
        InputWidget.getInfo("单据类型", ThingsHelper.getType(s1.SType, s1.State)));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getInfo("当前状态", getSS()));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getInfo("创建时间", Helper.DateTimeFormat(s1.CreateTM)));

    list3
        .add(ThingsHelper.thingsCard2(s1.TName, s1.Unit, s1.TType, s1.Storage));
    list4.add(InputWidget.getInfo("数量", s1.Amount.toString()));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getInfo("执行人", s1.UserName));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getInfo("登记人", CacheHelper.getUserName(s1.UserID)));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getInfo2("备注", s1.Remarks));
    list4.add(InputWidget.getDivider());

    if (s1.State >= 1) {
      list5.add(InputWidget.getQMTile2("审核签字", true, s1.LDQM, s1.LDTM));
    }

    list1.add(InputWidget.inCard2(list2));
    list1.add(InputWidget.inCard2(list3));
    list1.add(InputWidget.inCard2(list4));

    if (s1.State == 0 && Helper.AuthPB("物资审核")) {
      list5.add(
          InputWidget.getButton("审核签字", () => onTapButton(context, "审核签字")));
    }
    list1.add(InputWidget.inCard2(list5));
    if (s1.SType == 2 && s1.State == 1 && Helper.UserPB(s1.UserID)) {
      list6.add(InputWidget.getButton("归还", () => onTapButton(context, "归还")));
      list1.add(InputWidget.inCard2(list6));
    }

    return list1;
  }

  void onTapButton(BuildContext context, String xx) {
    if (xx == "审核签字") {
      showPanel(context, signIt1, "审核签字");
    }

    if (xx == "归还") {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return DialogWidget.getDialog(
              context, "确认归还", "点击确定键将归还该条借出单！  \r\n名称：" + s1.TName, () async {
            s1.Amount2 = 0;
            s1.State = 2;
            await Inventory.Update(s1);
            await loadData();
          });
        },
      );
    }
  }

  signIt1(String imgId) async {
    s1.State = 1;
    s1.LDTM = Helper.DateTimeNowString();
    s1.LDQM = imgId;

    await Inventory.Update(s1);
    loadData();
    //await Helper.sendMessage("审核通知", "18884230190");
  }

  void showPanel(
      BuildContext context, SignaturedCallback checkIt, String title) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(checkIt, title);
      },
    );
  }

  CleanInfoFields(BuildContext context) => Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: getWidgetList(context),
        ),
      );
}
